package com.atguigu.cloud.controller;

import com.atguigu.cloud.dto.PayDto;
import com.atguigu.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

//    public static final String PaymentSrv_URL = "http://localhost:8001";

//    引入服务名称调用接口, 默认是有多个服务, 需要在RestTemplate加上注解@LoadBalanced开启负载均衡进行轮询进行调用
    public static final String PaymentSrv_URL = "http://cloud-payment-service"; // 服务注册中心上的微服务名称

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/pay/add")
    public ResultData addOrder(PayDto payDto) {
        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDto, ResultData.class);
    }

    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id, ResultData.class);
    }

    @GetMapping(value = "consumer/pay/get/info")
    public String getInfoByConsul() {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
    }

}
